﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Timers;
using System.Configuration;
using FPModel;
using log4net;
using System.Threading;

namespace WSFacebookPoster
{
    public partial class FacebookService : ServiceBase
    {
        private static readonly log4net.ILog _logger = LogManager.GetLogger(typeof(FacebookService));
        private System.Timers.Timer facebookTimer;
        Thread _thread;

        public FacebookService()
        {
            InitializeComponent();
            facebookTimer = new System.Timers.Timer();
        }

        protected override void OnStart(string[] args)
        {
            new Thread(Initialize).Start();
        }

        internal void Initialize()
        {
            log4net.Config.XmlConfigurator.Configure();

            int timeInterval = Int32.Parse(ConfigurationManager.AppSettings["TimeInterval"]);

            _logger.InfoFormat("Facebook service interval value: (sn) {0} (dk) {1}", timeInterval, timeInterval / 60);

            // Convert to miliseconds
            facebookTimer.Interval = timeInterval * 1000;
            facebookTimer.Enabled = true;
            facebookTimer.Elapsed += new ElapsedEventHandler(FacebookTimer_Elapsed);
            facebookTimer.Start();
        }

        protected override void OnStop()
        {

        }

        void FacebookTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            // facebook timer elapsed
            // send facebook post to page
            // connect to webservice to send
            new FacebookQuery().Test();
            
        }
    }
}
